package com.lincoln.cloud.servercenter.abstracter;

import com.lincoln.cloud.servercenter.interfaces.BaseServerInfo;
import com.lincoln.cloud.servercenter.interfaces.ServerPoolContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 默认的服务器池容器实现
 *
 * @author lincoln
 */
public class MapServerPoolContainer implements ServerPoolContainer {
    private static final Logger logger = LoggerFactory.getLogger(MapServerPoolContainer.class);

    /**
     * map实现
     */
    private static Map<String, Map<Integer, BaseServerInfo>> serverInfoMap;

    public MapServerPoolContainer() {
        serverInfoMap = new ConcurrentHashMap<>();
    }

    @Override
    public BaseServerInfo findById(int id) {
        BaseServerInfo res = null;
        for (Map<Integer, BaseServerInfo> group : serverInfoMap.values()) {
            res = group.get(id);
            if (res != null) {
                break;
            }
        }
        return res;
    }

    @Override
    public List<BaseServerInfo> findByName(String name) {
        List<BaseServerInfo> res = new ArrayList<>();
        if (serverInfoMap.get(name) != null) {
            Map<Integer, BaseServerInfo> group = serverInfoMap.get(name);
            for (BaseServerInfo info : group.values()) {
                res.add(info);
            }
        }
        return res;
    }

    @Override
    public List<BaseServerInfo> findAll() {
        List<BaseServerInfo> res = new ArrayList<>();
        for (Map<Integer, BaseServerInfo> group : serverInfoMap.values()) {
            res.addAll(group.values());
        }
        return res;
    }

    @Override
    public boolean addServer(BaseServerInfo serverInfo) {
        if (serverInfo.getPrimaryKey() == null || serverInfo.getName() == null || "".equals(serverInfo.getName()) || findById(serverInfo.getPrimaryKey()) != null) {
            return false;
        }
        Map<Integer, BaseServerInfo> group = serverInfoMap.get(serverInfo.getName());
        if (group == null) {
            group = new ConcurrentHashMap<>(2);
            serverInfoMap.put(serverInfo.getName(), group);
            group = serverInfoMap.get(serverInfo.getName());
        }
        group.put(serverInfo.getPrimaryKey(), serverInfo);
        return false;
    }

    @Override
    public boolean deleteServer(int id) {
        BaseServerInfo res;
        for (Map<Integer, BaseServerInfo> group : serverInfoMap.values()) {
            res = group.get(id);
            if (res != null) {
                group.remove(id);
                res.logout();
                logger.info("id=" + id + "设备退出!");
            }
        }
        return true;
    }

    @Override
    public boolean deleteServer(String name) {
        if (name == null || "".equals(name)) {
            return true;
        }
        Map<Integer, BaseServerInfo> group = serverInfoMap.get(name);
        if (group == null) {
            return true;
        }
        for (BaseServerInfo baseServerInfo : group.values()) {
            baseServerInfo.logout();
        }
        serverInfoMap.remove(name);
        return true;
    }
}
